﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Text;
using System.Windows.Threading;

namespace tianya
{
    public class OnResultEventArgs
    {
        public OnResultEventArgs(string s) { Text = s; }
        public String Text { get; private set; } // readonly
    }
    public class WebRequestHelper
    {
       public WebRequestHelper(){}
        // Declare the delegate (if using non-generic pattern).
        public delegate void OnResultEventHandler(object sender, OnResultEventArgs e);

        // Declare the event.
        public event OnResultEventHandler OnResultEvent;

        // Wrap the event in a protected virtual method
        // to enable derived classes to raise the event.
        protected virtual void RaiseOnResultEvent(string resutl)
        {
            // Raise the event by using the () operator.
            OnResultEvent(this, new OnResultEventArgs(resutl));
        }

        public static System.Threading.ManualResetEvent allDone = null;
        const int BUFFER_SIZE = 1024*1024;
        public void SendRequest(string URL)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL.ToString());
            RequestState myRequestState = new RequestState();
            myRequestState.request = request;
            IAsyncResult result = (IAsyncResult)request.BeginGetResponse(new AsyncCallback(RespCallback), myRequestState);
        }
        private void RespCallback(IAsyncResult asynchronousResult)
        {
            try
            {
                // State of request is asynchronous.
                RequestState myRequestState = (RequestState)asynchronousResult.AsyncState;
                HttpWebRequest myHttpWebRequest2 = myRequestState.request;
                myRequestState.response = (HttpWebResponse)myHttpWebRequest2.EndGetResponse(asynchronousResult);

                // Read the response into a Stream object.
                Stream responseStream = myRequestState.response.GetResponseStream();
                myRequestState.streamResponse = responseStream;
                byte[] responseString = new byte[responseStream.Length];
                responseStream.Read(responseString, 0, Convert.ToInt32(responseStream.Length));

                string result = Encoding.UTF8.GetString(responseString, 0, responseString.Length);
                if (!string.IsNullOrEmpty(result))
                {
                    responseStream.Close();
                    RaiseOnResultEvent(result);
                }
            }
            catch (Exception e)
            {
                // Need to handle the exception
            }
            finally
            {
                
            }
        }
     
    }
}
